|
|
|
|
|
|
|
it has a valid value in the JobTitle column. Notice that you have not required that the value be unique. That would require every person in the database to have a unique job title. Instead, you allow duplicate job titles in this column. In real life, you would probably want to check the value entered here against a list of valid job titles. That involves creating a foreign key relationship using the CONSTRAINT keyword. Read the next section for more on CONSTRAINTs. |
|
|
|
|
|
|
|
|
Managing Relationships with CONSTRAINTs |
|
|
|
|
|
|
|
|
CONSTRAINTs are really the same as indexes from the standpoint of SQL statements. The CONSTRAINT keyword is used to create indexes that add data integrity to your database. You must use the CONSTRAINT keyword with the CREATE TABLE or ALTER TABLE SQL statement. There is no such thing in Microsoft Access Jet SQL as CREATE CONSTRAINT. |
|
|
|
|
|
|
|
|
There are three forms of the CONSTRAINT clause: |
|
|
|
|
|
|
|
|
Microsoft Access SQL syntax does not allow you to use the IGNORE NULL or DISALLOW NULL keywords within the CONSTRAINT clause. If you want to create data integrity indexes that include the IGNORE NULL or DISALLOW NULL keywords, you have to use the CREATE INDEX keyword to define your index. |
|
|
|
|
|
|
|
|
Using the PRIMARY KEY CONSTRAINT |
|
|
|
|
|
|
|
|
The most commonly used CONSTRAINT clause is the PRIMARY KEY CONSTRAINT. This is used to define the column (or set of columns) that contains the primary key for the table. The SQL-VB5 script in Listing 13.8 creates a new database and a single table that contains two fields, one of which is the primary key column for the table. The other field is a MEMO field. MEMO fields can contain any type of free-form text and cannot be used in any CONSTRAINT or INDEX definition. |
|
|
|
|
|
|
|
|
Listing 13.8. Testing the PRIMARY KEY CONSTRAINT. |
|
|
|
|
|
|
|
|
//
// sqlvb04.sqv - Test CONSTRAINT SQL keyword
//
// create a database
dbMake sqlvb04.mdb;
// create jobs title table
CREATE TABLE JobsTable
(JobTitle TEXT (20) CONSTRAINT PKJobTitle PRIMARY KEY,
JobDesc MEMO |
|
|
|
|
|